import PySimpleGUI as sg

def readConfigFile(configFile):
    with open(configFile,'r',encoding='utf-8') as f:
        lines = f.readlines()
    f.close()
    return ''.join(lines)
def writeConfigFile(outconfigFile,data):
    with open(outconfigFile,'w+',encoding='utf-8') as f:
        f.write(data)
    f.close()
def FormatToOneLine(recv):
    return str(recv).strip().replace('\n','').replace('\r','').replace('\t','').replace(' ','')
def FormatToJosn(recv):
    result =[]
    qiege = recv.replace('{','').replace('}','').split(',')
    count = 0
    result.append('{\n')
    while count <len(qiege):
        if count == len(qiege)-1:
            result.append('\t' + str(qiege[count]).strip() + '\n')
        else:
            result.append('\t'+str(qiege[count]).strip()+',\n')
        count+=1
    result.append('}')
    return ''.join(result)
def Frame():
    # 定义布局
    layout = [
        [sg.FileBrowse(button_text="输入文件", target='-IN-',button_color='green'), sg.In(tooltip='请先点击前方按钮添加输入文件路径！',key='-IN-')],  # 文本输入框先指定一个key，用target显示目标路径的key
        [sg.FileBrowse(button_text="输出文件", target='-OUT-'), sg.In(tooltip='请先点击前方按钮添加输出文件路径！',key='-OUT-')],
        [sg.Button('JsonToLine'),sg.Button('LineToJson')]
    ]

    window = sg.Window('tttttt',layout,background_color='black')
    ask_status = False
    while True:
        if ask_status == False:
            status = sg.popup_ok_cancel('第一步请先选择输入文件和输出文件，\n否则后续流程无法实现哦！',title='友情提醒',font=('幼圆',20,'bold'))
        else:
            status = 'OK'

        print(status)
        if status == 'OK':
            event,vaule = window.read()
            if event == None or sg.WINDOW_CLOSED:
                break
            if event =='JsonToLine':
                configFile = vaule['-IN-']
                outconfigFile = vaule['-OUT-']
                if configFile != '' and outconfigFile !='':
                    recv = readConfigFile(configFile)
                    recv = FormatToOneLine(recv)
                    print(recv)
                    writeConfigFile(outconfigFile, recv)
                else:
                    print('请选择输入文件和输出文件～')
                print('文件路径为：'+configFile)
            if event == 'LineToJson':
                configFile = vaule['-IN-']
                outconfigFile = vaule['-OUT-']
                if configFile != '' and outconfigFile != '':
                    recv = readConfigFile(configFile)
                    recv = FormatToJosn(recv)
                    print(recv)
                    writeConfigFile(outconfigFile,recv)
                else:
                    print('请选择输入文件和输出文件～')
                print('文件路径为：'+configFile)
        else:
            sg.popup_error('没有选择同意将无法继续后续.\n系统将关闭，下次见,拜拜~',title='错误提示',text_color='red',font=('幼圆',20,'bold'))
            print('不同意则关闭，禁止使用~')
            break
        ask_status = True

if __name__ == '__main__':
    #recv = readConfigFile('./configFile.txt')
    #recv = FormatToOneLine(recv)
    #recv = FormatToJosn(recv)
    #print(recv)
    Frame()